package jpsearchcore;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;

import jpsearchcore.ExternalDescription.EDTagName;
import jpsearchcore.ExternalDescriptonType.LiteralValue;
import jpsearchcore.ExternalDescriptonType.ResourceValue;
import jpsearchcore.ExternalDescriptonType.StructuredValue;
import jpsearchcore.ExternalDescriptonType.TagName;

public class ExternalDescription {

	private EDTagName tagName;
	private EDLiteralValue literalValue;
	private EDResourceValue resourceValue;
	private EDStructuredValue structuredValue;

	public ExternalDescription (){}
	
	public ExternalDescription (ExternalDescriptonType edt){

		//TagName
		TagName tn = edt.getTagName();
		tagName = new EDTagName(tn);

		//LiteralValue
		LiteralValue lv = edt.getLiteralValue();
		literalValue = new EDLiteralValue(lv);

		//ResourceValue
		ResourceValue rv = edt.getResourceValue();
		resourceValue = new EDResourceValue(rv);

		//StructuredValue
		StructuredValue sv = edt.getStructuredValue();	
		structuredValue = new EDStructuredValue(sv);

	}

	public ExternalDescription(EDTagName tagName, EDLiteralValue literalValue,
			EDResourceValue resourceValue, EDStructuredValue structuredValue) {
		this.tagName = tagName;
		this.literalValue = literalValue;
		this.resourceValue = resourceValue;
		this.structuredValue = structuredValue;
	}
	
	public ExternalDescription(ExtDescription e){
		tagName = new EDTagName(e.getTnFromNameSpace(),e.getTnFromNameSpacePrefix(),e.getTnContent());
		literalValue = new EDLiteralValue(e.getLvFromControlledVocabularyOfLiterals(),
				e.getLvFromGrammarOfLiterals(), e.getLvContent());
		resourceValue = new EDResourceValue(e.getRvFromNameSpace(), e.getRvContent());
		List<Object> o = new ArrayList<Object>();
		if(e.getSvContent() instanceof String){
			String s = StringEscapeUtils.unescapeXml(e.getSvContent().toString());
			if(!StringUtils.isBlank(s))
				o.add(s);
		}
		else
			o.add(e.getSvContent());
		structuredValue = new EDStructuredValue(e.getSvFromNameSpace(), o);
	}

	public EDTagName getTagName() {
		return tagName;
	}

	public void setTagName(EDTagName tagName) {
		this.tagName = tagName;
	}

	public EDLiteralValue getLiteralValue() {
		return literalValue;
	}

	public void setLiteralValue(EDLiteralValue literalValue) {
		this.literalValue = literalValue;
	}

	public EDResourceValue getResourceValue() {
		return resourceValue;
	}

	public void setResourceValue(EDResourceValue resourceValue) {
		this.resourceValue = resourceValue;
	}

	public EDStructuredValue getStructuredValue() {
		return structuredValue;
	}

	public void setStructuredValue(EDStructuredValue structuredValue) {
		this.structuredValue = structuredValue;
	}

	@Override
	public String toString() {
		return "tagName=" + tagName + "\nliteralValue="
				+ literalValue + "\nresourceValue=" + resourceValue
				+ "\nstructuredValue=" + structuredValue + "]";
	}

	public static class EDTagName{

		private String FromNameSpace = "";
		private String FromNameSpacePrefix = "";
		private String Content = "";
		
		public EDTagName(){}

		public EDTagName(TagName tn){
			try{
				FromNameSpace = tn.getFromNamespace();
			}catch(NullPointerException e){}

			try{
				FromNameSpacePrefix = tn.getFromNamespacePrefix();
			}catch(NullPointerException e){}

			try{
				Content = tn.getContent();
			}catch(NullPointerException e){}

		}
		
		public EDTagName(String fromNameSpace, String fromNameSpacePrefix,
				String content) {
			super();
			FromNameSpace = fromNameSpace;
			FromNameSpacePrefix = fromNameSpacePrefix;
			Content = content;
		}

		public String getFromNameSpace() {
			return FromNameSpace;
		}
		public String getFromNameSpacePrefix() {
			return FromNameSpacePrefix;
		}		
		public String getContent() {
			return Content;
		}

		public void setFromNameSpace(String fromNameSpace) {
			FromNameSpace = fromNameSpace;
		}
		public void setFromNameSpacePrefix(String fromNameSpacePrefix) {
			FromNameSpacePrefix = fromNameSpacePrefix;
		}
		public void setContent(String content) {
			Content = content;
		}

		@Override
		public String toString() {
			return "EDTagName [FromNameSpace=" + FromNameSpace
					+ ", FromNameSpacePrefix=" + FromNameSpacePrefix
					+ ", Content=" + Content + "]";
		}
	}

	public static class EDLiteralValue{
		
		private String FromControlledVocabularyOfLiterals = "";
		private String FromGrammarOfLiterals = "";
		private String Content = "";

		public EDLiteralValue(){}
		
		public EDLiteralValue(LiteralValue lv){
			try{
				FromControlledVocabularyOfLiterals = lv.getFromControlledVocabularyOfLiterals();
			}
			catch(NullPointerException e){}	
			try{
				FromGrammarOfLiterals = lv.getFromGrammarOfLiterals();
			}
			catch(NullPointerException e){}	
			try{
				Content = lv.getContent();
			}
			catch(NullPointerException e){}	
		}
		
		
		public EDLiteralValue(String fromControlledVocabularyOfLiterals,
				String fromGrammarOfLiterals, String content) {
			super();
			FromControlledVocabularyOfLiterals = fromControlledVocabularyOfLiterals;
			FromGrammarOfLiterals = fromGrammarOfLiterals;
			Content = content;
		}

		public String getFromControlledVocabularyOfLiterals() {
			return FromControlledVocabularyOfLiterals;
		}
		public String getFromGrammarOfLiterals() {
			return FromGrammarOfLiterals;
		}
		public String getContent() {
			return Content;
		}
		
		
		public void setFromControlledVocabularyOfLiterals(
				String fromControlledVocabularyOfLiterals) {
			FromControlledVocabularyOfLiterals = fromControlledVocabularyOfLiterals;
		}
		public void setFromGrammarOfLiterals(String fromGrammarOfLiterals) {
			FromGrammarOfLiterals = fromGrammarOfLiterals;
		}
		public void setContent(String content) {
			Content = content;
		}

		@Override
		public String toString() {
			return "EDLiteralValue [FromControlledVocabularyOfLiterals="
					+ FromControlledVocabularyOfLiterals
					+ ", FromGrammarOfLiterals=" + FromGrammarOfLiterals
					+ ", Content=" + Content + "]";
		}
	}

	public static class EDResourceValue {

		private String FromNameSpace = "";
		private String Content = "";
		
		public EDResourceValue(){}

		public EDResourceValue(ResourceValue rv){
			try{
				FromNameSpace = rv.getFromNamespace();
			}
			catch(NullPointerException e){}	

			try{
				Content = rv.getContent();
			}
			catch(NullPointerException e){}
		}
		
		

		public EDResourceValue(String fromNameSpace, String content) {
			super();
			FromNameSpace = fromNameSpace;
			Content = content;
		}



		public String getFromNameSpace() {
			return FromNameSpace;
		}

		public void setFromNameSpace(String fromNameSpace) {
			FromNameSpace = fromNameSpace;
		}

		public String getContent() {
			return Content;
		}

		public void setContent(String content) {
			Content = content;
		}
		
		@Override
		public String toString() {
			return "EDResourceValue [FromNameSpace=" + FromNameSpace
					+ ", Content=" + Content + "]";
		}
	}
	
	public static class EDStructuredValue{

		private String FromNameSpace = "";
		private List<Object> Content = new ArrayList<Object>();
		
		public EDStructuredValue(){}
		
		public EDStructuredValue(StructuredValue sv){

			try{
				FromNameSpace = sv.getFromNamespace();
			}
			catch(NullPointerException e){}	

			try{
				Content.addAll(sv.getContent());
			}
			catch(NullPointerException e){}	
		}
		
		 
		
		public EDStructuredValue(String fromNameSpace, List<Object> content) {
			FromNameSpace = fromNameSpace;
			Content = content;
		}



		public String getFromNameSpace() {
			return FromNameSpace;
		}



		public List<Object> getContent() {
			return Content;
		}



		public void setFromNameSpace(String fromNameSpace) {
			FromNameSpace = fromNameSpace;
		}

		@Override
		public String toString() {
			return "EDStructureValue [FromNameSpace=" + FromNameSpace
					+ ", Content=" + Content + "]";
		}

	}
}
